package icu.qx.wm.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.wm.domain.WmStorageArea;
import icu.qx.wm.service.IWmStorageAreaService;
import icu.qx.wm.vo.WarehouseFormVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/wm/warehouse/area")
public class WmStorageAreaController extends BaseController {
    private final IWmStorageAreaService wmStorageAreaService;

    @Autowired
    public WmStorageAreaController(IWmStorageAreaService wmStorageAreaService) {
        this.wmStorageAreaService = wmStorageAreaService;
    }

    /**
     * 查询库区列表
     */
    @RequiresPermissions("wm:warehouse:area:list")
    @GetMapping("/list")
    public TableDataInfo list(WmStorageArea wmStorageArea) {
        startPage();
        List<WmStorageArea> list = wmStorageAreaService.selectWmStorageAreaList(wmStorageArea);
        return getDataTable(list);
    }

    /**
     * 导出库区列表
     */
    @RequiresPermissions("wm:warehouse:area:export")
    @Log(title = "库区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmStorageArea wmStorageArea) {
        List<WmStorageArea> list = wmStorageAreaService.selectWmStorageAreaList(wmStorageArea);
        ExcelUtil<WmStorageArea> util = new ExcelUtil<>(WmStorageArea.class);
        util.exportExcel(response, list, "库区数据");
    }

    /**
     * 获取库区详细信息
     */
    @RequiresPermissions("wm:warehouse:area:query")
    @GetMapping(value = "/{saCode}")
    public AjaxResult getInfo(@PathVariable("saCode") String saCode) {
        return success(wmStorageAreaService.selectWmStorageAreaBySaCode(saCode));
    }

    /**
     * 新增库区
     */
    @RequiresPermissions("wm:warehouse:area:add")
    @Log(title = "库区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmStorageArea wmStorageArea) {
        return toAjax(wmStorageAreaService.insertWmStorageArea(wmStorageArea));
    }

    /**
     * 修改库区
     */
    @RequiresPermissions("wm:warehouse:area:edit")
    @Log(title = "库区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmStorageArea wmStorageArea) {
        return toAjax(wmStorageAreaService.updateWmStorageArea(wmStorageArea));
    }

    /**
     * 删除库区
     */
    @RequiresPermissions("wm:warehouse:area:remove")
    @Log(title = "库区", businessType = BusinessType.DELETE)
    @DeleteMapping("/{saCodes}/{slCount}")
    public AjaxResult remove(@PathVariable(name = "saCodes") String[] saCodes, @PathVariable(name = "slCount", required = false) Integer[] slCount) {
        return toAjax(wmStorageAreaService.deleteWmStorageAreaBySaCodes(saCodes, slCount));
    }

    /**
     * 查询库存编码
     */
    @RequiresPermissions("wm:warehouse:area:query")
    @GetMapping("/list/codes")
    public AjaxResult getSaCodeWithSlWCode(WarehouseFormVo warehouseFormVo) {
        return success(wmStorageAreaService.selectStorageAreaCode(warehouseFormVo));
    }
}
